package com.ljh.onlinetest.controller.admin;

import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.configuration.property.SystemConfig;
import com.ljh.onlinetest.service.FileUpload;
import com.ljh.onlinetest.service.UserService;
import com.ljh.onlinetest.viewmodel.admin.file.UeditorConfigVM;
import com.ljh.onlinetest.viewmodel.admin.file.UploadResultVM;
import org.apache.http.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/**
 * @author ljh
 * @ClassName UploadController.java
 * @createTime 2022年02月22日
 */
@RestController("UploadController")
@RequestMapping("/api/admin/upload")
public class UploadController extends BaseApiController {

    private final FileUpload fileUpload;
    private final SystemConfig systemConfig;
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    private static final String IMAGE_UPLOAD = "imgUpload";
    private static final String IMAGE_UPLOAD_FILE = "upFile";
    private final UserService userService;

    @Autowired
    public UploadController(FileUpload fileUpload, SystemConfig systemConfig, UserService userService) {
        this.fileUpload = fileUpload;
        this.systemConfig = systemConfig;
        this.userService = userService;
    }

    @RequestMapping("/configAndUpload")
    public Object upload(HttpServletRequest request, HttpServletResponse response){
        String action = request.getParameter("action");
        if (action.equals(IMAGE_UPLOAD)) {
            try {
                MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                MultipartFile file = multipartHttpServletRequest.getFile(IMAGE_UPLOAD_FILE);
                long size = file.getSize();
                String originalFilename = file.getOriginalFilename();
                String filePath;
                try (InputStream inputStream = file.getInputStream()){
                    filePath = fileUpload.uploadFile(inputStream,size,originalFilename);
                }
                String imgType = originalFilename.substring(originalFilename.lastIndexOf("."));
                UploadResultVM uploadResultVM = new UploadResultVM();
                uploadResultVM.setOriginal(originalFilename);
                uploadResultVM.setName(originalFilename);
                uploadResultVM.setUrl(filePath);
                uploadResultVM.setSize(file.getSize());
                uploadResultVM.setType(imgType);
                uploadResultVM.setState("SUCCESS");
                return uploadResultVM;
            }catch (IOException e) {
                logger.error(e.getMessage(),e);
            }
        }else {
            UeditorConfigVM ueditorConfigVM = new UeditorConfigVM();
            ueditorConfigVM.setImageActionName(IMAGE_UPLOAD);
            ueditorConfigVM.setImageFieldName(IMAGE_UPLOAD_FILE);
            ueditorConfigVM.setImageMaxSize(2048000L);
            ueditorConfigVM.setImageAllowFiles(Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));
            ueditorConfigVM.setImageCompressEnable(true);
            ueditorConfigVM.setImageCompressBorder(1600);
            ueditorConfigVM.setImageInsertAlign("none");
            ueditorConfigVM.setImageUrlPrefix("");
            ueditorConfigVM.setImagePathFormat("");
            return ueditorConfigVM;
        }
        return null;
    }
}
